package com.mediaideas.videoengine;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.mediaideas.videoengine.constants.AppMessageDef;
import com.mediaideas.videoengine.utils.VideoEngineUtils;

/**
 * Created by Jonathan Kranz on 2016/3/5.
 * 混音音频处理封装类
 */
public class AudioProc {
    // 混音状态-成功
    public final static int STATUS_AUDIO_PROC_SUCCESS = 0X0001;
    // 混音状态-正在处理
    public final static int STATUS_AUDIO_PROC_SAVING = 0X0002;
    // 混音状态-失败
    public final static int STATUS_AUDIO_PROC_FAILED = -1;
    protected final static String TAG = "AudioProc";
    // 是否已经load so库
    protected static boolean NATIVE_LIBRARY_LOADED = false;

    // 静态块-加载so库
    static {
        loadNativeLibraries();
    }

    protected Context mContext;
    protected Handler mHandler;
    // 是否已经取消保存
    protected boolean mCancelSaving = false;
    // 保存进度
    protected int mSavingProgress;
    // 混音参数
    protected AudioMixerParams mAudioMixerParams;

    public AudioProc(Context context, Handler handler) {
        mContext = context;
        mHandler = handler/*new Handler(Looper.getMainLooper())*/;
    }

    protected static void loadNativeLibraries() {
//        ReportUtils.leaveBreadcrumb("VideoEditor:loadNativeLibraries");
        NATIVE_LIBRARY_LOADED = VideoEngineUtils.loadVideoLibraries();
    }

    // 启动混音
    public int startConvertAudio(AudioMixerParams params) {
        if (!NATIVE_LIBRARY_LOADED) {
            throw new IllegalStateException("MEDIAPROC-SDK are  not loaded");
        }

        if (mCancelSaving) {
            Log.e(TAG, "startConvertAudio cancelSaving:" + mCancelSaving);
            return 0;
        }
        mSavingProgress = 0;
        mAudioMixerParams = params;
        Log.e(TAG, "startConvertAudio");
        // 调用JNI方法进行底层库混音处理
        return nativeStartConvertAudio(mAudioMixerParams);
    }

    public boolean getSavingStatus() {
        if (!NATIVE_LIBRARY_LOADED) {
            throw new IllegalStateException("MEDIAPROC-SDK are  not loaded");
        }
        return nativeGetSavingStatus();
    }

    // 取消混音并销毁底层库分配的资源
    public int cancelConvertAudio() {
        if (!NATIVE_LIBRARY_LOADED) {
            throw new IllegalStateException("MEDIAPROC-SDK are  not loaded");
        }
        mCancelSaving = false;
        int reslutCancel = nativeCancelSaving();
        int resultDestroy = nativeDestroy();
        Log.e(TAG, "cancelConvertAudio");
        return reslutCancel == 0 && resultDestroy == 0 ? 0 : -1;
    }

    // 销毁底层库分配的资源
    public int destroy() {
        if (!NATIVE_LIBRARY_LOADED) {
            throw new IllegalStateException("MEDIAPROC-SDK are  not loaded");
        }
        mCancelSaving = false;
        return nativeDestroy();
    }

    // 进度提示-底层库回调方法(不能修改方法名及参数类型)
    final protected void onNotifyTransCodeProgress(int progress) {
        if (mHandler == null) {
            return;
        }

//        long curTick = System.currentTimeMillis();
//        if(curTick - mLastUpdateTick < 400)
//            return;
//        mLastUpdateTick = curTick;

//        int progress = (int) (100 * currentTime * 1000 / mVideoProcParams.getVideoDuration());

//        Log.e("Test", "Progress:" + progress);
        if (mSavingProgress != progress) {
            mSavingProgress = progress;
            Message msg = Message.obtain(mHandler, AppMessageDef.MSG_JNI_CALLBACK_NOTIFY_PROGRESS);
            msg.arg1 = mSavingProgress;
            mHandler.sendMessage(msg);
        }
    }

    // 混音结构-底层库回调方法(不能修改方法名及参数类型)
    final protected void onNotifyTransCodeResult(int result) {
        if (mHandler == null)
            return;
        Log.e(TAG, "result:" + result);
        Message msg = Message.obtain(mHandler, AppMessageDef.MSG_JNI_CALLBACK_NOTIFY_RESULT);
        msg.arg1 = result;
        mHandler.sendMessage(msg);
    }

    // JNI方法-开始底层混音处理
    public native int nativeStartConvertAudio(AudioMixerParams params);

    // JNI方法-获取底层混音状态
    public native boolean nativeGetSavingStatus();

    // JNI方法-取消底层混音处理
    public native int nativeCancelSaving();

    // JNI方法-销毁底层混音分配的资源
    public native int nativeDestroy();
}
